'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var rebelRecordModel = require('../../../models/rebelRecordModel');
var rebelData = require('../../../libs/war/rebelData');
var battleData = require('../../../libs/war/battleData');
var rebelConf = require('../../../config/rebelConf');
var constantUtil = require('../../../util/constantUtil');
var PlayerManager = require('../../../libs/playerManager');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 进入副本协议
 * 要给客户端下发的数据:
 * 1.敌方的建筑
 * 2.敌方的英雄,小兵
 * 3.我方的英雄,小兵
 */
Handler.prototype.enter = function (msg, session, next) {
    var role_id = session.uid;
    var rebel_id = msg.rebel_id;
    if (!rebel_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    if (!rebelConf.getRebelConf(rebel_id)) {
        return next(null, {
            code: Code.REBEL_NOT_EXIST
        });
    }

    var role_model;
    var rebel_model;
    var rebel_record_model;
    var atk_data;
    var def_data;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'rebel', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                rebel_model = player_manager.rebel;

                // 判断今天是否已经通关过了
                if (!!rebel_model.isTodayPassRebel(rebel_id)) {
                    return next(null, {
                        code: Code.REBEL_TODAY_HAVE_PASS
                    });
                }

                // 判断这个来袭的之前的难度是否完成
                if (!rebel_model.isPassPreRebel(rebel_id)) {
                    return next(null, {
                        code: Code.REBEL_PRE_NOT_PASS
                    });
                }

                def_data = _getDef(rebel_id);

                // 我方数据
                _getAtk(role_id, cb);
            },
            function (data, cb) {
                atk_data = data;
                atk_data.rebel_skill_list = rebelConf.getRebelSkillList();

                // 存储战斗记录
                rebelRecordModel.addRecord(
                    role_id,
                    rebel_id,
                    atk_data,
                    def_data,
                    cb);
            },
            function (model, cb) {
                rebel_record_model = model;
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    record_id: rebel_record_model.getRecordId(),
                    atk: atk_data,
                    def: def_data
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, cb) {
    battleData.getData(role_id, cb);
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (rebel_id) {
    return rebelData.getData(rebel_id);
};
